package dk.ihk.ssgroup5;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dk.ihk.ssgroup5.dto.User;
import dk.ihk.ssgroup5.service.LoginService;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username, password;
		username = request.getParameter("userId");
		password = request.getParameter("password");
		
		LoginService loginService = new LoginService();
		boolean loginIsDone = loginService.authenticate(username, password);
		if (!loginIsDone) {
			response.sendRedirect("DesignServlet?pageId=1&navId=1");
			return;				
		}
		
		else {
			User user = loginService.getUserDetails(username);
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			
			response.sendRedirect(request.getContextPath() + "/DesignServlet?pageId=10&navId=2");
			return;
		}
	}

}
